<?php 
// Define the file as an iCalendar file
header("Content-Type: text/Calendar");
// Give the file a name and force download
header("Content-Disposition: inline; filename=calendar.ics");
// Header of ics file
echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID:PHP\n";
echo "METHOD:PUBLISH\n";

//Print each event
/*echo "BEGIN:VEVENT\n";
echo "DTSTART;VALUE=DATE:20090228\n";
echo "DTSTAMP:20090305T042925Z\n";
echo "DTEND;VALUE=DATE:20090307\n";
echo "UID:jim\n";
echo "DESCRIPTION;LANGUAGE=en;ENCODING=QUOTED-PRINTABLE:THIS IS MY DESC\n";
echo "SUMMARY;LANGUAGE=en;ENCODING=QUOTED-PRINTABLE:THIS IS MY SUMMARY\n";
echo "END:VEVENT\n";*/

foreach($events->result() as $event){
	echo "BEGIN:VEVENT\n";
	echo "DTSTART;TZID=US-Eastern:" . date("Ymd\\THis",strtotime($event->start)) . "\n";
	echo "DTEND;TZID=US-Eastern:" . date("Ymd\\THis",strtotime($event->end)) . "\n";
	echo "UID:" . $event->id . "\n";
	echo "DTSTAMP:" . date("Ymd\\THis") . "\n";
//	echo "CATEGORIES;LANGUAGE=en;ENCODING=QUOTED-PRINTABLE:Seminar - ECE\n";
	$event->description = str_replace("\n","\\n",$event->description);
	echo "DESCRIPTION;LANGUAGE=en;ENCODING=QUOTED-PRINTABLE:". $event->description . "\n";
	echo "SUMMARY;LANGUAGE=en;ENCODING=QUOTED-PRINTABLE:" . $event->name ."\n";
	echo "URL:" . site_url('calendar/eventDetails/'. $event->id) . "\n";
	echo "END:VEVENT\n";
}
echo "END:VCALENDAR\n";
?>